Completed
Push — master ( d1844c...194c7c )
by
unknown
03:08
created

page.js ➔ ... ➔ ???   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
import mkdirp from 'mkdirp'
2
import {
3
  Util,
0 ignored issues
show
Unused Code introduced by
The variable Util seems to be never used. Consider removing it.
Loading history...
4
  cmsData,
5
  FileParser,
6
  fileUtils,
7
  config,
8
  Page,
9
  cmsTemplate,
10
  cleanSlug
11
} from '../../cli'
12
13
var page = function (req, res, next) {
0 ignored issues
show
Unused Code introduced by
The parameter next is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
14
  var filePath = cleanSlug(req.query.filePath)
15
  filePath = fileUtils.getFilePath(filePath)
16
  var html = (req.query.html) ? true : false
17
  var json = null
18
  var editor = false
19
  if(typeof req.body.json !== 'undefined' && req.body.json !== null) {
20
    editor = true
21
    if(typeof req.body.json === 'string') {
22
      json = JSON.parse(req.body.json)
23
    }else {
24
      json = req.body.json
25
    }
26
  }
27
28
  if(typeof filePath !== 'undefined' && filePath !== null) {
29
    var jsonPath = null
30
    var linkPath = null
31
32
    var filePathTest = cmsData.revision.getDocumentRevision(req.query.filePath)
33
    if(typeof filePathTest !== 'undefined' && filePathTest !== null) {
34
      // filePath = filePathTest.path
35
      jsonPath = filePathTest.path
36
      linkPath = filePathTest.abe_meta.link
37
    }
38
39
    if(jsonPath === null || !fileUtils.isFile(jsonPath)) { 
40
      res.status(404).send('Not found')
41
      return
42
    } 
43
44
    if(typeof json === 'undefined' || json === null) {
45
      json = FileParser.getJson(jsonPath)
46
    }
47
    
48
    let meta = config.meta.name
49
50
    var template = ''
51
    if(typeof json[meta] !== 'undefined' && json[meta] !== null && json[meta] !== ''
52
      && json[meta].template !== 'undefined' && json[meta].template !== null && json[meta].template !== '') {
53
      template = json[meta].template
54
    }else {
55
      template = req.params[0]
56
    }
57
    var text = cmsTemplate.template.getTemplate(template)
58
59
    if (!editor) {
60
61
      cmsData.source.getDataList(fileUtils.removeLast(linkPath), text, json)
62
        .then(() => {
63
          var page = new Page(template, text, json, html)
64
          res.set('Content-Type', 'text/html')
65
          res.send(page.html)
66
        }).catch(function(e) {
67
          console.error(e)
68
        })
69
    }else {
70
      text = cmsData.source.removeDataList(text)
71
      var page = new Page(template, text, json, html)
72
      res.set('Content-Type', 'text/html')
73
      res.send(page.html)
74
    }
75
  }else {
76
    // not 404 page if tag abe image upload into each block
77
    if(/upload%20image/g.test(req.url)) {
78
      var b64str = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='
79
      var img = new Buffer(b64str, 'base64')
0 ignored issues
show
Bug introduced by
The variable Buffer seems to be never declared. If this is a global, consider adding a /** global: Buffer */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
80
81
      res.writeHead(200, {
82
        'Content-Type': 'image/jpeg',
83
        'Content-Length': img.length
84
      })
85
      res.end(img) 
86
    }else {
87
      res.status(404).send('Not found')
88
    }
89
  }
90
}
91
92
export default page